home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvitovdu / unix / c / systemif.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  9KB  |  375 lines

  1. /* Original Author:         Andrew Trevorrow
  2.    Implementation: Modula-2 under VAX/UNIX 4.2 BSD
  3.    Date Started:   June, 1986
  4.  
  5.    Description:
  6.    SYSDEP: Implements InitSysInterface() the routine that gets the
  7.    DVItoVDU command line and extracts the DVI file name and option values.
  8.  
  9.    This version converted to C and ported to BSD and System V UNIX by
  10.    some chaps at Kernel Technology up to September 1989.  Contact
  11.    mjh@uk.co.kernel (Mark J. Hewitt) with bug fixes etc.
  12.  
  13.    Involved were:    Mark J. Hewitt
  14.                Dave Dixon
  15.             Marc Hadley
  16. */
  17.  
  18. #include "def.h"
  19. #ifdef USG
  20. #include <string.h>
  21. #else
  22. #include <strings.h>
  23. #endif /* USG */
  24. #include "systemif.h"
  25.  
  26. static char *sccsid[] = "@(#)systemif.c    1.1";
  27.  
  28. /* TYPE UNITS = (IC,CM,MM,PC,PT,PX);*/
  29.  /* SYSDEP: changed in to IC; compiler confused with IN, even with -sk!!! */
  30. #define IC 1
  31. #define CM 2
  32. #define MM 3
  33. #define PC 4
  34. #define PT 5
  35. #define PX 6
  36. #define UNITS short
  37.  
  38. #ifndef PKFONTDIR
  39. #define PKFONTDIR "/usr/local/lib/tex/fonts/pk"
  40. #endif
  41.  
  42. #ifndef HELPFILELOC
  43. #define HELPFILELOC "/usr/local/lib/tex/dvitovdu.hlp" 
  44. #endif
  45.  
  46. extern int NumParameters;
  47.  
  48. char  option;            /* current command option */
  49. stringvalue value;        /* current option's value */
  50. int   length;            /* current value's length */
  51.  
  52. /**********************************************************************/
  53.  
  54. short   ExplicitExt (fname, len)
  55. char *fname;
  56. int   len;
  57.  
  58. {
  59.  
  60. /* SYSDEP: Check for an extension of the form ".*" where * is any string.
  61.    len is length of fname.
  62.    If "." found then TRUE is returned, otherwise FALSE.
  63. */
  64.  
  65.   while (len > 0)        /* search backwards looking for . */
  66.   {
  67.     len--;
  68.     if (fname[len] == '.')
  69.     {
  70.       return TRUE;
  71.     }
  72.   }
  73.   return FALSE;
  74. }
  75.  
  76. /**********************************************************************/
  77.  
  78. void GetCardinal (n)
  79. unsigned int *n;
  80. {
  81.  
  82. /* If current value represents a positive integer then return via n. */
  83.  
  84.   if ((sscanf (value, "%d", n) != 1) || ((long)n <= 0L))
  85.   {
  86.     WriteString ("Bad -");
  87.     Write (option);
  88.     WriteString (" value: ");
  89.     WriteString (value);
  90.     WriteLn ();
  91.     WriteString ("Specify a positive integer.");
  92.     WriteLn ();
  93.     RestoreTerminal ();
  94.     exitprog (1);
  95.   }
  96. }
  97.  
  98. /**********************************************************************/
  99.  
  100. void Get2Dimension (r, un)
  101. float  *r;
  102. UNITS * un;
  103. /* A valid dimension consists of a positive integer or real number followed
  104.    by a two-letter unit: IN, CM, MM, PC, PT or PX (or in lowercase).
  105.    If current value represents a valid dimension, we return number part in r
  106.    and units part in un.
  107. */
  108. {
  109.   int   i;
  110.   char  ch1, ch2;
  111.  
  112. /* extract un */
  113.   if (length > 1)
  114.     i = length - 1;
  115.   else
  116.     i = 1;
  117.   if (((value[i - 1] == 'I') || (value[i - 1] == 'i')) &&
  118.       ((value[i] == 'N')     || (value[i] == 'n')))
  119.   {
  120.     *un = IC;
  121.   }
  122.   else
  123.     if (((value[i - 1] == 'C') || (value[i - 1] == 'c')) &&
  124.     ((value[i] == 'M')     || (value[i] == 'm')))
  125.     {
  126.       *un = CM;
  127.     }
  128.     else
  129.       if (((value[i - 1] == 'M') || (value[i - 1] == 'm')) &&
  130.       ((value[i] == 'M')     || (value[i] == 'm')))
  131.       {
  132.     *un = MM;
  133.       }
  134.       else
  135.     if (((value[i - 1] == 'P') || (value[i - 1] == 'p')) &&
  136.         ((value[i] == 'C')     || (value[i] == 'c')))
  137.     {
  138.       *un = PC;
  139.     }
  140.     else
  141.       if (((value[i - 1] == 'P') || (value[i - 1] == 'p')) &&
  142.           ((value[i] == 'T')     || (value[i] == 't')))
  143.       {
  144.         *un = PT;
  145.       }
  146.       else
  147.         if (((value[i - 1] == 'P') || (value[i - 1] == 'p')) &&
  148.         ((value[i] == 'X')     || (value[i] == 'x')))
  149.         {
  150.           *un = PX;
  151.         }
  152.         else
  153.         {
  154.           WriteString ("Bad units in -");
  155.           Write (option);
  156.           WriteString (" value: ");
  157.           WriteString (value);
  158.           WriteLn ();
  159.           WriteString ("Last two letters should be IN, CM, MM, PC, PT or PX.");
  160.           WriteLn ();
  161.           RestoreTerminal ();
  162.           exitprog (1);
  163.         }
  164.   ch1 = value[i - 1];        /* remember letters in units */
  165.   ch2 = value[i];
  166.   value[i] = '\0';        /* remove units */
  167.   value[i - 1] = '\0';
  168.   if ((sscanf (value, "%f", r) != 1) || (*r <= 0.0))
  169.   {
  170.     value[i - 1] = ch1;        /* restore units */
  171.     value[i] = ch2;
  172.     WriteString ("Bad -");
  173.     Write (option);
  174.     WriteString (" value: ");
  175.     WriteString (value);
  176.     WriteLn ();
  177.     WriteString ("Specify a positive dimension.");
  178.     WriteLn ();
  179.     RestoreTerminal ();
  180.     exitprog (1);
  181.   }
  182. }
  183.  
  184. /**********************************************************************/
  185.  
  186. void InitSysInterface ()
  187. {
  188.  
  189. /* If an option appears more than once then we return the last value. */
  190.  
  191.   unsigned int  i;
  192.   int num;
  193.   UNITS unitwd, unitht;
  194.   float   realwd, realht;
  195.   extern char *getenv();
  196.  
  197. /* SYSDEP: initialize option values with defaults */
  198.   resolution = 300;        /* LaserWriter resolution */
  199.   unitwd = IC;            /* paper dimensions in inches */
  200.   unitht = IC;
  201.   realwd = 8.3;            /* A4 paper is 8.3" wide */
  202.   realht = 11.7;        /* A4 paper is 11.7" high */
  203.   mag = 0;            /* use DVI mag */
  204.   (void) strcpy (vdu, getenv ("TERM"));/* set vdu to value of TERM */
  205.   if (vdu[0] == '\0')
  206.   {
  207.     (void) strcpy (vdu, "unknown");
  208.   }
  209.   (void) strcpy (dummyfont, "cmr10.1500pxl");
  210.   (void) strcpy (fontdir, PKFONTDIR);
  211.   (void) strcpy (helpname, HELPFILELOC);
  212.   DVIname[0] = '\0';        /* SYSDEP: empty string */
  213.   num = 1;
  214.   while (num < NumParameters)
  215.   {
  216.     length = GetParameter (num, value);
  217.     num++;
  218.     if (value[0] == '-')
  219.     {
  220.       if (length > 1)
  221.       {
  222.     option = value[1];
  223.       }
  224.       else
  225.       {
  226.     option = ' ';
  227.       }
  228.       if ((option != 'r') && (option != 'm') && (option != 'x') &&
  229.       (option != 'y') && (option != 'v') && (option != 'd') &&
  230.       (option != 'h') && (option != 'f'))
  231.       {
  232.     WriteString ("Unknown option: -");
  233.     Write (option);
  234.     WriteLn ();
  235.     RestoreTerminal ();
  236.     exitprog (1);
  237.       }
  238.     /* allow things like -vvis500 */
  239.       if (length > 2)
  240.       {                /* shift value left 2 places */
  241.     for (i = 0; i <= length - 1; i++)
  242.     {
  243.       value[i] = value[i + 2];
  244.     }
  245.     length = length - 2;
  246.     value[length] = '\0';    /* SYSDEP: terminate with NULL */
  247.       }
  248.       else            /* value is next parameter */
  249.       {
  250.     length = GetParameter (num, value);
  251.     num++;
  252.     if (length <= 0)
  253.     {
  254.       WriteString ("Missing value after -");
  255.       Write (option);
  256.       WriteLn ();
  257.       RestoreTerminal ();
  258.       exitprog (1);
  259.     }
  260.       }
  261.       switch (option)
  262.       {
  263.     case 'r': 
  264.       GetCardinal (&resolution);
  265.       break;
  266.     case 'm': 
  267.       GetCardinal (&mag);
  268.       break;
  269.     case 'x':
  270.       Get2Dimension (&realwd, &unitwd);
  271.       break;
  272.     case 'y':
  273.       Get2Dimension (&realht, &unitht);
  274.       break;
  275.     case 'v': 
  276.       (void) strcpy (vdu, value);
  277.       break;
  278.     case 'd': 
  279.       (void) strcpy (dummyfont, value);
  280.       break;
  281.     case 'h':
  282.       (void) strcpy (helpname, value);
  283.       break;
  284.     case 'f':
  285.       (void) strcpy (fontdir, value);
  286.       break;
  287.     /* bad string values will be detected in other modules */
  288.       }
  289.     }
  290.     else
  291.     {
  292.       (void) strcpy (DVIname, value);
  293.       if (!ExplicitExt (DVIname, length))/* append .dvi */
  294.       {
  295.     if ((length + 3) <= MAXSTRLEN - 1)
  296.     {
  297.       DVIname[length] = '.';
  298.       DVIname[length + 1] = 'd';
  299.       DVIname[length + 2] = 'v';
  300.       DVIname[length + 3] = 'i';
  301.       if (length + 4 <= MAXSTRLEN - 1)
  302.       /* SYSDEP: append NULL */
  303.       {
  304.         DVIname[length + 4] = '\0';
  305.       }
  306.     }
  307.     else            /* user has given a mighty long file name! */
  308.     {
  309.       WriteString ("DVI file name too long: ");
  310.       WriteString (DVIname);
  311.       WriteLn ();
  312.       RestoreTerminal ();
  313.       exitprog (1);
  314.     }
  315.       }
  316.     /* bad DVIname will be detected upon open in main module */
  317.     }
  318.   }
  319.   if (DVIname[0] == '\0')
  320.   {
  321.   /* no file name on command line */
  322.     WriteString ("Missing DVI file name!");
  323.     WriteLn ();
  324.     RestoreTerminal ();
  325.     exitprog (1);
  326.   }
  327. /* set paperwd and paperht only after resolution has been decided */
  328.   switch (unitwd)
  329.   {
  330.     case IC: 
  331.       paperwd = (unsigned) (realwd * ((float) resolution) + 0.5);
  332.       break;
  333.     case CM: 
  334.       paperwd = (unsigned) ((realwd / 2.54) * ((float) resolution) + 0.5);
  335.       break;
  336.     case MM: 
  337.       paperwd = (unsigned) ((realwd / 25.4) * ((float) resolution) + 0.5);
  338.       break;
  339.     case PT: 
  340.       paperwd = (unsigned) ((realwd / 72.27) * ((float) resolution) + 0.5);
  341.       break;
  342.     case PC: 
  343.       paperwd = (unsigned) ((realwd / 72.27) * 12.0 * ((float) resolution) + 0.5);
  344.       break;
  345.     case PX: 
  346.       paperwd = (unsigned) (realwd + 0.5);
  347.       break;
  348.   }
  349.   switch (unitht)
  350.   {
  351.     case IC: 
  352.       paperht = (unsigned) (realht * ((float) resolution) + 0.5);
  353.       break;
  354.     case CM: 
  355.       paperht = (unsigned) ((realht / 2.54) * ((float) resolution) + 0.5);
  356.       break;
  357.     case MM: 
  358.       paperht = (unsigned) ((realht / 25.4) * ((float) resolution) + 0.5);
  359.       break;
  360.     case PT: 
  361.       paperht = (unsigned) ((realht / 72.27) * ((float) resolution) + 0.5);
  362.       break;
  363.     case PC: 
  364.       paperht = (unsigned) ((realht / 72.27) * 12.0 * ((float) resolution) + 0.5);
  365.       break;
  366.     case PX: 
  367.       paperht = (unsigned) (realht + 0.5);
  368.       break;
  369.   }
  370. }
  371.  
  372.  
  373.  
  374.  
  375.